//
//  HZPeiziQueryOwnCell.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/12/6.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziQueryOwnCell.h"

#define kOwnedStockNameFont [UIFont systemFontOfSize:15]
#define kOwnedStockCodeFont [UIFont systemFontOfSize:13]
#define kUpDownMargin 5
#define kLeftRightMargin 20
#define kDateW 100
#define kTitleH 35
#define kBottomStatusH 45

@interface HZPeiziQueryOwnCell()
@property (nonatomic, strong)UIView  *pBgView;
@property (nonatomic, strong)UIView  *pLineView;
@property (nonatomic, strong)UILabel *pTitleLabel;
@property (nonatomic, strong)UILabel *pStatusLabel;
@property (nonatomic, strong)NSMutableArray *pLeftArray;
@property (nonatomic, strong)NSMutableArray *pRightArray;
@property (nonatomic, strong)UIView  *pBottomLineView;

@property (nonatomic, strong)UILabel  *pDetailLabel;
@property (nonatomic, strong)UIButton *pDetailBtn;
@property (nonatomic, strong)UIButton *pLeftBtn;
@property (nonatomic, strong)UIButton *pRightBtn;

@property (nonatomic, strong)HZPeiziTradeStockListModel *pTempModel;
@end

@implementation HZPeiziQueryOwnCell

+ (HZPeiziQueryOwnCell *)cellTableView:(UITableView *)tableView
                                indexPath:(NSIndexPath *)indexPath
                                cellModel:(HZPeiziTradeStockListModel *)cellModel
{
    static NSString *ID = @"peiQueryOwnCell";
    HZPeiziQueryOwnCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[HZPeiziQueryOwnCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell p_commonInit];
    }
    [cell p_updateDataWithmodel:cellModel indexPath:indexPath];
    
    return cell;
}
- (void)p_commonInit
{
    _pLeftArray = [NSMutableArray array];
    _pRightArray = [NSMutableArray array];
    _pBgView = [UIView new];
    _pLineView = [UIView new];
    _pTitleLabel = [UILabel new];
    _pStatusLabel = [UILabel new];
    _pBottomLineView = [UIView new];
    _pDetailLabel = [UILabel new];
    _pDetailBtn = [UIButton new];
    _pLeftBtn = [UIButton new];
    _pRightBtn = [UIButton new];
    
    _pTitleLabel.font = kOwnedStockNameFont;
    _pTitleLabel.textColor = kMYHexToRGB(0x333333, 1);
    _pStatusLabel.font = kOwnedStockNameFont;
    _pStatusLabel.textAlignment = NSTextAlignmentRight;
    _pStatusLabel.textColor = kMYHexToRGB(0xf15059, 1);
    _pStatusLabel.backgroundColor = [UIColor whiteColor];
    [_pDetailBtn addTarget:self action:@selector(detailOnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _pDetailLabel.text = @"查看详情";
    _pDetailLabel.textColor = kMYHexToRGB(0x999999, 1);
    _pDetailLabel.font = kOwnedStockNameFont;
    _pLeftBtn.backgroundColor = kREDColor;
    _pRightBtn.backgroundColor = kMYHexToRGB(0x55bfe8, 1);
    _pLeftBtn.layer.cornerRadius = 25/2;
    _pRightBtn.layer.cornerRadius = 25/2;
    [_pLeftBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_pRightBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _pBgView.backgroundColor = [UIColor whiteColor];
    _pLineView.backgroundColor = kMYHexToRGB(0xe4e4e4, 1);
    _pTitleLabel.backgroundColor = [UIColor whiteColor];
    _pTitleLabel.textAlignment = NSTextAlignmentLeft;
    self.backgroundColor = [UIColor clearColor];
    _pBottomLineView.backgroundColor = kMYHexToRGB(0xe4e4e4, 1);
    
    [self.contentView addSubview:_pBgView];
    [_pBgView addSubview:_pLineView];
    [_pLineView addSubview:_pTitleLabel];
    [_pLineView addSubview:_pStatusLabel];
    [_pBgView addSubview:_pDetailBtn];
    [_pBgView addSubview:_pLeftBtn];
    [_pBgView addSubview:_pRightBtn];
    [_pBgView addSubview:_pBottomLineView];
    [_pDetailBtn addSubview:_pDetailLabel];
    
    _pDetailLabel.hidden = YES;
    
    NSArray *leftNameArray = @[@"参考盈亏",@"参考盈亏率",@"参考市值",@"持仓数量",@"可用数量",@"参考成本",@"最新价格"];
    for (NSString *name in leftNameArray) {
        UILabel *pLeftLabel = [UILabel new];
        UILabel *pRightLabel = [UILabel new];
        pLeftLabel.text = name;
        pLeftLabel.font = kOwnedStockCodeFont;
        pLeftLabel.textAlignment = NSTextAlignmentLeft;
        pRightLabel.font = kOwnedStockCodeFont;
        pRightLabel.textAlignment = NSTextAlignmentRight;
        pLeftLabel.textColor = kMYHexToRGB(0x999999, 1);
        pRightLabel.textColor = kMYHexToRGB(0x333333, 1);
        [_pBgView addSubview:pLeftLabel];
        [_pBgView addSubview:pRightLabel];
        [_pLeftArray addObject:pLeftLabel];
        [_pRightArray addObject:pRightLabel];
    }
    [self p_initConstraints];
    
}
- (void)p_initConstraints
{
    [_pBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(kUpDownMargin);
        make.left.mas_equalTo(self).offset(kLeftRightMargin);
        make.right.mas_equalTo(self).offset(-kLeftRightMargin);
        make.bottom.mas_equalTo(self).offset(-kUpDownMargin);
    }];
    [_pLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pBgView);
        make.left.mas_equalTo(_pBgView).offset(kLeftRightMargin);
        make.right.mas_equalTo(_pBgView).offset(-kLeftRightMargin);
        make.height.mas_equalTo(kTitleH);
    }];
    [_pTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pLineView);
        make.left.mas_equalTo(_pLineView);
        make.right.mas_equalTo(_pStatusLabel.mas_left);
        make.bottom.mas_equalTo(_pLineView).offset(-0.5);
    }];
    [_pStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pTitleLabel);
        make.right.mas_equalTo(_pBgView).offset(-kLeftRightMargin);
        make.height.mas_equalTo(_pTitleLabel);
        make.width.mas_equalTo(100);
    }];
    
    UILabel *preObj = _pLeftArray[0];
    [preObj mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pLineView.mas_bottom);
        make.left.mas_equalTo(_pBgView).offset(kLeftRightMargin);
        make.right.mas_equalTo(_pBgView).offset(-kLeftRightMargin);
    }];
    [_pRightArray[0] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(preObj);
        make.left.mas_equalTo(_pBgView).offset(kLeftRightMargin);
        make.right.mas_equalTo(_pBgView).offset(-kLeftRightMargin);
        make.height.mas_equalTo(preObj);
        make.width.mas_equalTo(preObj);
    }];
    for (int i = 1; i < _pLeftArray.count; i++) {
        UILabel *leftLabel = _pLeftArray[i];
        UILabel *rightLabel = _pRightArray[i];
        
        [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(preObj.mas_bottom);
            make.left.mas_equalTo(_pBgView).offset(kLeftRightMargin);
            make.right.mas_equalTo(_pBgView).offset(-kLeftRightMargin);
            make.height.mas_equalTo(preObj);
        }];
        [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(leftLabel);
            make.left.mas_equalTo(leftLabel.mas_right);
            make.right.mas_equalTo(_pBgView).offset(-kLeftRightMargin);
            make.height.mas_equalTo(leftLabel);
            make.width.mas_equalTo(leftLabel);
        }];
        preObj = leftLabel;
    }
    [_pDetailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(preObj.mas_bottom);
        make.left.mas_equalTo(_pBgView).offset(kLeftRightMargin);
        make.bottom.mas_equalTo(_pBgView);
        make.height.mas_equalTo(kBottomStatusH);
        make.width.mas_equalTo(65);
    }];
    [_pDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(_pDetailBtn);
    }];
    [_pRightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_pDetailBtn);
        make.centerX.mas_equalTo(_pBgView.mas_right).offset(-kLeftRightMargin - 65/2);
        make.width.mas_equalTo(65);
        make.height.mas_equalTo(25);
    }];
    [_pLeftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_pDetailBtn);
        make.centerX.mas_equalTo(_pBgView.mas_left).offset(kLeftRightMargin + 65/2);
        make.width.mas_equalTo(65);
        make.height.mas_equalTo(25);
    }];
    [_pBottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pDetailBtn);
        make.left.mas_equalTo(_pBgView).offset(kLeftRightMargin);
        make.right.mas_equalTo(_pBgView).offset(-kLeftRightMargin);
        make.height.mas_equalTo(0.5);
    }];
}
- (void)p_updateDataWithmodel:(HZPeiziTradeStockListModel *)model indexPath:(NSIndexPath *)indexPath
{
    _pTempModel = model;
    [self p_cleaOldData];
    [self p_updateNewData:model];
}
- (void)p_updateNewData:(HZPeiziTradeStockListModel *)model
{    
    _pTitleLabel.text = [NSString stringWithFormat:@"%@--%@",model.stkcode, model.stkname];

    NSArray *contentArray = @[[NSString stringWithFormat:@"%@元",model.income],
                              [NSString stringWithFormat:@"%@",model.incomeratio],
                              [NSString stringWithFormat:@"%@元",model.marketval],
                              [NSString stringWithFormat:@"%@股",model.amt],
                              [NSString stringWithFormat:@"%@股",model.enable],
                              [NSString stringWithFormat:@"%@元/股",model.price],
                              [NSString stringWithFormat:@"%@元/股",model.lastprice],
                              ];
    [_pRightArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *content = contentArray[idx];
        UILabel *rightLabel = (UILabel *)obj;
        rightLabel.text = content;
        if (idx == 0) {
            if ([model.income floatValue] < 0) {
                rightLabel.textColor = kGREENColor;
            }else if ([model.income floatValue] > 0) {
                rightLabel.textColor = kREDColor;
            }else {
                rightLabel.textColor = kLIGHTGRAYColor;
            }
            if ([[rightLabel.text substringFromIndex:rightLabel.text.length - 1] isEqual:@"元"]) {
                NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:rightLabel.text];
                [attStr addAttribute:NSForegroundColorAttributeName value:kMYHexToRGB(0x333333, 1) range:NSMakeRange(rightLabel.text.length-1, 1)];
                rightLabel.attributedText = attStr;
            }
        }
        
        if (idx == 1) {
            if ([model.incomeratio floatValue] < 0) {
                rightLabel.textColor = kGREENColor;
            }else if ([model.incomeratio floatValue] > 0) {
                rightLabel.textColor = kREDColor;
            }else {
                rightLabel.textColor = kLIGHTGRAYColor;
            }
        }
    }];

    [_pLeftBtn setTitle:@"买入" forState:UIControlStateNormal];
    [_pRightBtn setTitle:@"卖出" forState:UIControlStateNormal];
}
- (void)p_cleaOldData
{
    [_pTitleLabel setText:@""];
    for (UILabel *rigthLabel in _pRightArray) {
        rigthLabel.text = @"";
    }
    _pRightBtn.backgroundColor = kMYHexToRGB(0x3399ff, 1);
}

- (void)detailOnClick
{
    LogicRouterBlock excuteBlock = [self.logicManager matchLogicBlock:kLogicRouterExcute_clickList];
    if (excuteBlock) {
        excuteBlock(nil, _pTempModel);
    }
}
- (void)btnOnClick:(UIButton *)btn
{
    NSString *router = nil;
    if ([btn.currentTitle isEqual:@"卖出"]) {
        router = kLogicRouterPeiziSell;
    }else if ([btn.currentTitle isEqual:@"买入"]) {
        router = kLogicRouterPeiziBuy;
    }
    LogicRouterBlock excuteBlock = [self.logicManager matchLogicBlock:router];
    if (excuteBlock) {
        excuteBlock(nil, _pTempModel.stkcode);
    }
}

@end
